﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;

namespace KinectedMusic.Kinect
{
    public enum PostureStatus { BothHandsUp, BothHandsDown, LeftHandUp, RightHandUp, Other };

    public class PostureDetector
    {
        private PostureStatus postureStatus;

        public event EventHandler<PostureStatusArgs> RaisePostureStatusChange;

        public PostureDetector()
        {
            postureStatus = PostureStatus.Other;
        }

        public PostureStatus DetectPosture(Skeleton skeleton)
        {
            PostureStatus newPostureStatus;

            //if (BothHandsUp(skeleton))
            //{
            //    newPostureStatus = PostureStatus.BothHandsUp;
            //}
            //else if (BothHandsDown(skeleton))
            //{
            //    newPostureStatus = PostureStatus.BothHandsDown;
            //}
            //else
            if (LeftHandUp(skeleton))
            {
                newPostureStatus = PostureStatus.LeftHandUp;
            }
            else
            {
                newPostureStatus = PostureStatus.RightHandUp;
            }
            //else
            //{
            //    newPostureStatus = PostureStatus.Other;
            //}

            if (postureStatus != newPostureStatus)
            {
                postureStatus = newPostureStatus;

                EventHandler<PostureStatusArgs> handler = RaisePostureStatusChange;
                if (handler != null)
                {
                    handler(this, new PostureStatusArgs((PostureStatus)postureStatus));
                }
            }


            return newPostureStatus;
        }

        //private bool BothHandsUp(Skeleton skeleton)
        //{
        //    Joint handRight = skeleton.Joints[JointType.HandRight];
        //    Joint handLeft = skeleton.Joints[JointType.HandLeft];
        //    Joint hipLeft = skeleton.Joints[JointType.HipLeft];
        //    Joint hipRight = skeleton.Joints[JointType.HipRight];


        //    double minLowForHandsY = (hipLeft.Position.Y + hipRight.Position.Y) / 2.0;
        //    minLowForHandsY += .15;

        //    if ((handLeft.Position.Y > minLowForHandsY) && (handRight.Position.Y > minLowForHandsY))
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        //private bool BothHandsDown(Skeleton skeleton)
        //{
        //    Joint handRight = skeleton.Joints[JointType.HandRight];
        //    Joint handLeft = skeleton.Joints[JointType.HandLeft];
        //    Joint hipLeft = skeleton.Joints[JointType.HipLeft];
        //    Joint hipRight = skeleton.Joints[JointType.HipRight];

        //    double minLowForHandsY = (hipLeft.Position.Y + hipRight.Position.Y) / 2.0;
        //    minLowForHandsY += .15;

        //    if ((handLeft.Position.Y < minLowForHandsY) && (handRight.Position.Y < minLowForHandsY))
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        private bool LeftHandUp(Skeleton skeleton)
        {
            Joint handRight = skeleton.Joints[JointType.HandRight];
            Joint handLeft = skeleton.Joints[JointType.HandLeft];
            Joint hipLeft = skeleton.Joints[JointType.HipLeft];
            Joint hipRight = skeleton.Joints[JointType.HipRight];

            // TODO: CHANGE THIS!!
            //double minLowForHandsY = (hipLeft.Position.Y + hipRight.Position.Y) / 2.0;
            //minLowForHandsY += .15;

            if (handLeft.Position.Y > handRight.Position.Y)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //private bool RightHandUp(Skeleton skeleton)
        //{
        //    Joint handRight = skeleton.Joints[JointType.HandRight];
        //    Joint handLeft = skeleton.Joints[JointType.HandLeft];
        //    Joint hipLeft = skeleton.Joints[JointType.HipLeft];
        //    Joint hipRight = skeleton.Joints[JointType.HipRight];

        //    double minLowForHandsY = (hipLeft.Position.Y + hipRight.Position.Y) / 2.0;
        //    minLowForHandsY += .15;

        //    if ((handLeft.Position.Y < minLowForHandsY) && (handRight.Position.Y > minLowForHandsY))
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
    }
}
